<?php defined('_XR_EXEC') or die();
/**
 * ip  class.
 *
 * 
 * by zhoupeng 2012-4-25
 *
 */
class ip {
	# 得服务器IP
	static function server() {
		$val = self::env('REMOTE_ADDR');
		if ($val === self::env('SERVER_ADDR') && $ip = self::env('HTTP_PC_REMOTE_ADDR')) {
			return $ip;
		}
		return $val;
	}
	# 得客户端IP
	static function client(){
		if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown"))
		$ip = getenv("HTTP_CLIENT_IP");
		else if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown"))
		$ip = getenv("HTTP_X_FORWARDED_FOR");
		else if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown"))
		$ip = getenv("REMOTE_ADDR");
		else if (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown"))
		$ip = $_SERVER['REMOTE_ADDR'];
		else
		$ip = "unknown";
		return($ip);
	}
		
	static function &env($key, $default = null) {
		$val = null;
		if (isset ($_SERVER[$key])) {
			$val =  $_SERVER[$key];
		}
		elseif (isset ($_ENV[$key])) {
			$val =  $_ENV[$key];
		}
		elseif (getenv($key) !== false) {
			$val =  getenv($key);
		}
		if ($val !== null) {
			return $val;
		} else {
			return $default;
		}
	}
	
}
?>